<?php

namespace App\Http\Controllers;

use App\Activity;
use App\Round;
use Illuminate\Http\Request;
use App\Traits\RedPacket;

class RedPacketController extends Controller
{
    public function test()
    {
        $coupon = new RedPacket(100, 60, 1, 1, null);
        $res = $coupon->handleRandom();
//        $res = $coupon->handleFixed();
        $n = 0;
        foreach ($res['items'] as $val) {
            if ($val == 0) {
                $n ++ ;
            }
        }

//        print_r($res);
//        $data = $this->getRandMoney(10000, 60, 100);
        return response()->json(['data'=>$res, 'n' => $n]);
    }

    public function redPacket()
    {
        $activities = Activity::where(['state' => 1])->paginate(2);
        $rounds = Round::where(['state' => 1])->paginate(2);

        return view('redpacket.index', [
                'activity' => $activities,
                'round' => $rounds
        ]);
    }

    public function addActivity(Request $request)
    {
        $name = $request->input('name');
        $activity = new Activity();
        $activity->name = $name;
        $activity->state = 1;
        if ($activity->save()) {
            return redirect('redPacket');
        } else {
            return response()->json('failed');
        }
    }

    public function addRound(Request $request)
    {
        $name = $request->input('name');
        $activity = new Activity();
        $activity->name = $name;
        $activity->state = 1;
        if ($activity->save()) {
            return response()->json('success');
        } else {
            return response()->json('failed');
        }
    }
}
